package com.xiaoxin.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxin.lease.model.entity.*;
import com.xiaoxin.lease.model.enums.ItemType;
import com.xiaoxin.lease.web.admin.mapper.*;
import com.xiaoxin.lease.web.admin.service.*;
import com.xiaoxin.lease.web.admin.vo.attr.AttrValueVo;
import com.xiaoxin.lease.web.admin.vo.graph.GraphVo;
import com.xiaoxin.lease.web.admin.vo.room.RoomDetailVo;
import com.xiaoxin.lease.web.admin.vo.room.RoomItemVo;
import com.xiaoxin.lease.web.admin.vo.room.RoomQueryVo;
import com.xiaoxin.lease.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
@Transactional
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    RoomAttrValueService roomAttrValueService;

    @Autowired
    RoomFacilityService roomFacilityService;


    @Autowired
    RoomLabelService roomLabelService;


    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    RoomLeaseTermService roomLeaseTermService;

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    GraphInfoServiceImpl graphInfoServiceImpl;


    @Autowired
    AttrValueMapper attrValueMapper;


    @Autowired
    AttrKeyMapper attrKeyMapper;

    @Autowired
    RoomFacilityService rommFacilityService;

    @Autowired
    FacilityInfoMapper facilityInfoMapper;

    @Autowired
    FacilityInfoService facilityInfoService;


    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    RoomPaymentTypeService paymentTypeService;

    @Autowired
    PaymentTypeMapper paymentTypeMapper;

    @Autowired
    RoomLeaseTermService termService;


    @Autowired
    LeaseTermMapper leaseTermMapper;

    @Autowired
    LabelInfoService labelInfoService;

    @Autowired
    PaymentTypeService payTypeService;
    @Autowired
    LeaseTermService leaseTermService;


    @Override
    public void saveOrUpdateRoomSubmitVo(RoomSubmitVo roomSubmitVo) {
        /**
         * 1、保存房间room_info信息
         * 2、保存房间属性room_attr_value信息
         * 3、保存房间配套room_facility
         * 4、保存房间标签信息room_label
         * 5、保存房间支付方式信息room_label
         * 6、保存房间可选租期room_lease_term
         * 7、保存图片
         */
        Long roomIdNum = roomSubmitVo.getId();
        //根据id先判断房间是否存着，如果存在，则做更新操作，否则做新增操作。
        List<RoomInfo> roomInfos = super.list(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getId, roomIdNum));
        if (roomInfos != null) {
            //表面存在，则做更新操作
            super.saveOrUpdate(roomSubmitVo);


        }

        //不存在做保存操作，保存房间信息
        super.saveOrUpdate(roomSubmitVo);
        Long roomId = roomSubmitVo.getId();

//        RoomAttrValue roomAttrValue = new RoomAttrValue();
//        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
//        List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
//        for(Long atrId : attrValueIds){
//            roomAttrValue.setRoomId(roomId);
//            roomAttrValue.setAttrValueId(atrId);
//            roomAttrValueList.add(roomAttrValue);
//        }
//        roomAttrValueService.saveOrUpdateBatch(roomAttrValueList);
        roomSubmitVo.getAttrValueIds().forEach(attrValueId -> {
            RoomAttrValue roomAttrValue = new RoomAttrValue();
            roomAttrValue.setAttrValueId(attrValueId);
            roomAttrValue.setRoomId(roomId);
            roomAttrValueService.saveOrUpdate(roomAttrValue);
        });

        roomSubmitVo.getFacilityInfoIds().forEach(facilityId -> {
            RoomFacility roomFacility = new RoomFacility();
            roomFacility.setFacilityId(facilityId);
            roomFacility.setRoomId(roomId);
            roomFacilityService.saveOrUpdate(roomFacility);
        });

        roomSubmitVo.getLabelInfoIds().forEach(labelInfoId -> {
            RoomLabel roomLabel = new RoomLabel();
            roomLabel.setRoomId(roomId);
            roomLabel.setLabelId(labelInfoId);
            roomLabelService.save(roomLabel);
        });

        roomSubmitVo.getPaymentTypeIds().forEach(paymentTypeId -> {
            RoomPaymentType roomPaymentType = new RoomPaymentType();
            roomPaymentType.setPaymentTypeId(paymentTypeId);
            roomPaymentType.setRoomId(roomId);
            roomPaymentTypeService.save(roomPaymentType);
        });
        roomSubmitVo.getLeaseTermIds().forEach(leaseTermId -> {
            RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
            roomLeaseTerm.setLeaseTermId(leaseTermId);
            roomLeaseTerm.setRoomId(roomId);
            roomLeaseTermService.save(roomLeaseTerm);

        });
        roomSubmitVo.getGraphVoList().forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setItemId(roomId);
            graphInfoService.save(graphInfo);
        });


    }

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> iPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(iPage, queryVo);

    }

    @Override
    public RoomDetailVo getgetDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //1。查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        Long apartmentId = null;
        if (roomInfo != null) {
            apartmentId = roomInfo.getApartmentId();
        }
        //2、查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);

        //3、查询图片列表
        List<GraphVo> graphVoList = new ArrayList<>();
        List<GraphInfo> graphInfoList = graphInfoService.list(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, id));
        for (int i = 0; i < graphInfoList.size(); i++) {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfoList.get(i).getName());
            graphVo.setUrl(graphInfoList.get(i).getUrl());
            graphVoList.add(graphVo);
        }
        //4、查询基本属性列表
        List<AttrValueVo> attrValueVoList = new ArrayList<>();
        List<RoomAttrValue> attrValueList = roomAttrValueService.list(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
        for (int j = 0; j < attrValueList.size(); j++) {
            AttrValueVo attrValueVo = new AttrValueVo();
            Long attrValueId = attrValueList.get(j).getAttrValueId();
            AttrValue attrValue = attrValueMapper.selectById(attrValueId);
            if (attrValue != null) {
                AttrKey attrKey = attrKeyMapper.selectById(attrValue.getAttrKeyId());
                if (attrKey != null) {
                    attrValueVo.setAttrKeyName(attrKey.getName());
                }
                BeanUtils.copyProperties(attrValue, attrValueVo);
            }
            attrValueVoList.add(attrValueVo);
        }
        //5、查询配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoService.getRoomFaclity(id);
        //6、查询标签信息
        List<LabelInfo> labelInfoList = labelInfoService.getAllLabelInfo(id);

        //7、查询支付方式
        List<PaymentType> paymentTypeList = payTypeService.selectPaymentTypeList(id);
        //8、查询可选租期列表
        List<LeaseTerm> leaseTermList = leaseTermService.getLeaseTerm(id);
        if(roomInfo!=null){
            BeanUtils.copyProperties(roomInfo, roomDetailVo);
        }
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void removeRoom(Long id) {
        //1、根据id删除房间信息
        super.removeById(id);

        //2、根据id删除图片
        List<GraphInfo> graphInfoList = graphInfoService.list(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id));
        graphInfoService.removeBatchByIds(graphInfoList.stream().map(f -> f.getId()).toList());

        //3、删除基本属性
        List<RoomAttrValue> roomAttrValueList = roomAttrValueService.list(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
        roomAttrValueService.removeBatchByIds(roomAttrValueList.stream().map(f -> f.getId()).toList());

        //4、删除配套信息列表
        List<RoomFacility> roomFacilityList = roomFacilityService.list(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));

        roomFacilityService.removeBatchByIds(roomFacilityList.stream().map(f -> f.getId()).toList());

        //5、删除标签信息
        List<RoomLabel> roomLabelList = roomLabelService.list(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        roomLabelService.removeBatchByIds(roomLabelList.stream().map(f -> f.getId()).toList());

        //6、删除支付方式
        List<RoomPaymentType> roomPaymentTypeList = roomPaymentTypeService.list(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        roomPaymentTypeService.removeBatchByIds(roomPaymentTypeList.stream().map(f -> f.getId()).toList());

        //7、删除可选租期列表
        List<RoomLeaseTerm> roomLeaseTermList = roomLeaseTermService.list(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
        roomLeaseTermService.removeBatchByIds(roomLeaseTermList.stream().map(f -> f.getId()).toList());

    }
}




